import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

export const useHandleLoginEffect = (loginFromRef) => {
  const store = useStore()
  const router = useRouter()
  // 登录动作处理
  const loading = ref(false)

  // 数据源
  const loginForm = ref({
    username: 'super-admin',
    password: '123456'
  })
  const handleLogin = () => {
    loginFromRef.value.validate((valid) => {
      if (!valid) return

      loading.value = true
      store
        .dispatch('user/login', loginForm.value)
        .then(() => {
          loading.value = false
          // 登录后操作
          router.push('/')
        })
    })
  }
  return { loginForm, handleLogin }
}
